package server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Set;

import lottery.msgs.LotteryResult;

import phasebook.client.DirtyRegionManager;
import phasebook.client.RemotePhasebookService;
import phasebook.client.msgs.MessageInfo;
import phasebook.client.msgs.MovementInfo;
import phasebook.client.msgs.PhotoInfo;
import phasebook.client.msgs.UserInfo;
import phasebook.client.msgs.UserProfileInfo;

public class DevPhasebookService implements RemotePhasebookService, DirtyRegionManager{
	public static DevPhasebookService instance = new DevPhasebookService();
	
	public static DevPhasebookService getInstance(){
		return instance;
	}

	@Override
	public UserInfo[] getFriends(long userId) {
		return new UserInfo[]{
			new UserInfo(0,"Friend 0"),
			new UserInfo(1,"Friend 1"),
			new UserInfo(2,"Friend 2"),
		};
	}

	@Override
	public UserInfo[] getFriendRequests(long userId) {
		return new UserInfo[]{
			new UserInfo(0,"Friend Request 0"),
			new UserInfo(1,"Friend Request 1"),
			new UserInfo(2,"Friend Request 2"),
		};
	}

	@Override
	public UserInfo[] getMyFriendRequests(long userId) {
		return new UserInfo[]{
			new UserInfo(0,"MyFriend Request 0"),
			new UserInfo(1,"MyFriend Request 1"),
			new UserInfo(2,"MyFriend Request 2"),
		};
	}

	@Override
	public MovementInfo[] getFinancialMovements(long userId) {
		return new MovementInfo[]{
			new MovementInfo(0,100,"Wire Transfer",new Date()),
			new MovementInfo(0,-50,"Lottery Bet",new Date()),
			new MovementInfo(0,-50,"Lottery Winner",new Date())
		};
	}
	
	UserInfo me = new UserInfo(0,"me");
	UserInfo other = new UserInfo(1,"other");

	@Override
	public MessageInfo[] getMessages(long myId,long userId) {
		
		
		return new MessageInfo[]{
			new MessageInfo(0,me,other,"message 1",new Date(),new PhotoInfo(0,"c/car-527.jpg",new Date()),false),
			new MessageInfo(1,me,other,"message 2",new Date(),null,true),
			new MessageInfo(2,other,other,"message 3",new Date(),new PhotoInfo(1,"c/cars-531.jpg",new Date()),false),
			new MessageInfo(3,other,other,"message 4",new Date(),new PhotoInfo(2,"c/car-530.jpg",new Date()),true),
			new MessageInfo(4,other,other,"message 5",new Date(),null,false)
		};
	}

	@Override
	public UserProfileInfo getProfile(long userId) {
		return new UserProfileInfo(0,"My Name","myUsername","password","my@email.com",100.0, 1);
	}

	@Override
	public PhotoInfo[] getAlbum(long userId) {
		return new PhotoInfo[]{
			new PhotoInfo(0,"c/car-527.jpg",new Date()),
			new PhotoInfo(1,"c/car-529.jpg",new Date()),
			new PhotoInfo(2,"c/car-530.jpg",new Date()),
			new PhotoInfo(3,"c/car-533.jpg",new Date()),
			new PhotoInfo(4,"s/smart_cars-532.jpg",new Date()),
			new PhotoInfo(5,"c/cars-531.jpg",new Date()),
		};
	}

	@Override
	public UserInfo[] findUsers(long userId, String regEx) {
		return new  UserInfo[]{
			me,
			other,
			new UserInfo(2,"Found user 2")
		};
	}

	@Override
	public byte[] downloadPhoto(long photoId) {
		 ByteArrayOutputStream data;
		try {
			InputStream file = DevPhasebookService.class.getResourceAsStream("car.jpg");
			 data = new ByteArrayOutputStream();
			 
			 byte[] tmp = new byte[1024*1024];
			
			 int readed = 0;
			 while((readed=file.read(tmp))>=0)
				 data.write(tmp,0,readed);
			 return data.toByteArray();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public long registerUser(UserProfileInfo userProfile) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void updateUser(UserProfileInfo userProfile) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public long validateLogin(String userName, String password) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void publishMessage(MessageInfo newMessage) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void deleteMessage(long userId, long messageId) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void requestFriendship(long userId, long friendId) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void cancelFriendship(long userId, long friendId) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void cancelFriendshipRequest(long userId, long friendId)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void denyFriendshipRequest(long userId, long friendId)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void acceptFriendshipRequest(long userId, long friendId)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addPhoto(long userId, PhotoInfo photo, byte[] imageData)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void placeBet(long userId, double ammount, int result)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Set<DirtyRegion> waitForDirtyRegions(long myId) {
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
		}
		return null;
	}

	@Override
	public void publishDirtyRegions(long userId, Region... regions) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void subscribeSelf(long myId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void subscribeOtherUser(long myId, long userId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unsubscribeAll(long myId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unsubscribeOtherUser(long myId, long userId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateBalance(long userId, double amount, long accountId) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createFinancialMovement(long userId, double amount,
			String description) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processResults(LotteryResult[] lotteryResults) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyLotteryParticipants(LotteryResult[] lotteryResults)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
}
